package com.zjeav.lingjiao.ui.login;

import com.zjeav.lingjiao.base.baseBean.Result;
import com.zjeav.lingjiao.base.request.LoginRequest;
import com.zjeav.lingjiao.base.request.OtherLoginRequest;
import com.zjeav.lingjiao.base.response.LoginResponse;
import com.zjeav.lingjiao.base.service.LoginService;
import com.zjeav.lingjiao.base.service.QQLoginService;
import com.zjeav.lingjiao.base.service.WXLoginService;
import com.zjeav.lingjiao.base.tools.RetrofitInstance;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;

public class LoginModel {
    public void login(LoginRequest request, final OnLoginListener listener) {
        Retrofit retrofit = RetrofitInstance.getFormInstance();
        LoginService service = retrofit.create(LoginService.class);
        service.getLoginResponse(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Result loginResponse) {
                        listener.OnSuccess((LoginResponse) loginResponse.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.OnFailure(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void weixinLogin(OtherLoginRequest request, final OnLoginListener loginListener) {
        Retrofit retrofit = RetrofitInstance.getJsonInstance();
        WXLoginService service = retrofit.create(WXLoginService.class);
        service.getLoginResponse(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Result result) {
                        loginListener.OnSuccess((LoginResponse) result.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        loginListener.OnFailure(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    public void QQLogin(OtherLoginRequest request ,final OnLoginListener loginListener){
        Retrofit retrofit = RetrofitInstance.getJsonInstance();
        QQLoginService service = retrofit.create(QQLoginService.class);
        service.getLoginResponse(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Result result) {
                        loginListener.OnSuccess((LoginResponse) result.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        loginListener.OnFailure(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }
    public interface OnLoginListener {
        void OnSuccess(LoginResponse loginResponse);

        void OnFailure(Throwable throwable);

    }
}

